﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_InspectionController : MvcControllerBase
    {
        private InspectionItemBLL bll = new InspectionItemBLL();
        private InspectionBillBLL billbll = new InspectionBillBLL();
        private InspectionBillDetailBLL bdbll = new InspectionBillDetailBLL();
        #region
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult InspectionBillForm()
        {
            if (Request["keyValue"] == null)
            {
                ViewBag.Code = new CodeRuleBLL().GetBillCode( "", ((int)CodeRuleEnum.InspectionCode).ToString());
            }
            return View();
        }
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult InspectionBillDetail()
        {
            return View();
        }




        #endregion

        #region
        public ActionResult GetList(string Type)
        {
            var data = bll.GetList();
            if (!string.IsNullOrEmpty(Type))
            {
                data = data.Where(t => t.Type == Type).ToList();
            }
            return Content(data.ToJson());
        }
        public ActionResult GetEntity(string keyValue)
        {
            var data = bll.GetEntity(keyValue);
            return Content(data.ToJson());
        }

        public ActionResult GetItemTreeList(string Type)
        {
            var data = bll.GetList();
            if (!string.IsNullOrEmpty(Type))
            {
                data = data.Where(t => t.Type == Type).ToList();
            }
            var treeList = new List<TreeEntity>();
            foreach (InspectionItemEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.ParentId == item.Id) == 0 ? false : true;
                if (hasChildren == false)
                {
                    hasChildren = data.Count(t => t.Id == item.Id) == 0 ? false : true;
                    if (hasChildren == false)
                    {
                        continue;
                    }
                }
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.checkstate = 1;
                tree.showcheck = true;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.parentId = "0";
                tree.title = "item";
                tree.img = null;
                treeList.Add(tree);
            }
            return Content(treeList.TreeToJson());
        }


        public ActionResult GetBillList(string BillCode)
        {
            if (!string.IsNullOrEmpty(BillCode))
            {
                var data = billbll.GetList(BillCode);
                return Content(data.ToJson());
            }
            else
            {
                return Content("");
            }
        }

        public ActionResult GetBillEntity(string keyValue)
        {
            var data = billbll.GetEntity(keyValue);
            return Content(data.ToJson());
        }
        public ActionResult GetBillDetailList(string BillCode, string Type)
        {
            var dataDetail = bdbll.GetList(BillCode);
            var dataItem = bll.GetList();
            if (!string.IsNullOrEmpty(Type))
            {
                dataItem = dataItem.Where(t => t.Type == Type).ToList();
            }
            var data = dataItem.Join(dataDetail, x => x.Id, y => y.InspectionItemId, (x, y) => new
            {
                x.ResponDepts,
                y.InspectionItemName,
                x.Criterion,
                y.InspectionManName,
                y.Id,
                y.IsPass,
                y.InspectionDate,
                y.InspectionItemId,
                y.Memo
            });
            return Content(data.ToJson());
        }

        #endregion

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, InspectionItemEntity inspectionItemEntity)
        {
            bll.SaveForm(keyValue, inspectionItemEntity);
            return Success("操作成功。");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult RemoveForm(string keyValue)
        {
            bll.RemoveForm(keyValue);
            return Success("删除成功。");
        }

        [HttpPost]
        [AjaxOnly]
        public ActionResult InspectionBillDetaiForm(string keyValue, InspectionBillDetailEntity inspectionBillDetailEntity, string BillCode, string Type)
        {
            var dataDetail = bdbll.GetList(BillCode);
            var dataItem = bll.GetList();
            if (!string.IsNullOrEmpty(Type))
            {
                dataItem = dataItem.Where(t => t.Type == Type).ToList();
            }
            var data = dataItem.Join(dataDetail, x => x.Id, y => y.InspectionItemId, (x, y) => new
            {
                x.ResponDepts,
                y.InspectionItemName,
                x.Criterion,
                y.InspectionManName,
                y.Id,
                y.IsPass,
                y.InspectionDate,
                y.Memo
            });
            int State = 0;
            if (inspectionBillDetailEntity.IsPass == 0)
            {
                State = 3;
            }
            else
            {
                State = data.Where(t => t.Id != keyValue && t.IsPass != 1).Count() > 0 ? 1 : 2;
            }
            InspectionBillEntity inspectionBillEntity = billbll.GetEntity(BillCode);
            inspectionBillEntity.State = State;
            bdbll.SaveForm(keyValue, inspectionBillDetailEntity, inspectionBillEntity);
            return Success("操作成功。");
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveBillForm(string keyValue, string inspectionBillEntity, string Icodes, string Inames, string ItemIds)
        {
            InspectionBillEntity model = inspectionBillEntity.ToObject<InspectionBillEntity>();
            List<InspectionBillDetailEntity> listDetail = new List<InspectionBillDetailEntity>();
            if (string.IsNullOrEmpty(keyValue))
            {
                var data = billbll.GetList(model.BillId);
                switch (data.Count)
                {
                    case 0: if (model.InspectType != "1") return Error("请隐蔽工程验收！"); break;
                    case 1: if (data.FirstOrDefault().IsQualified == null || data.FirstOrDefault().IsQualified == 0)
                        {
                            return Error("隐蔽工程验收尚未通过！");
                        }
                        if (model.InspectType != "2") return Error("请选择整体验收！"); break;
                    case 2: if (data.FirstOrDefault().IsQualified == null || data.FirstOrDefault().IsQualified == 0)
                        {
                            return Error("整体验收尚未通过！");
                        }
                        if (model.InspectType != "3") return Error("请二次验收！"); break;
                    default:
                        return Error("不可再添加验收");

                }
            }
            if (!string.IsNullOrEmpty(ItemIds))
            {
                ItemIds = ItemIds.Substring(0, ItemIds.LastIndexOf(","));
                var Ids = ItemIds.Split(',');
                ItemIds = "";
                var dataDetail = bdbll.GetList(keyValue);
                foreach (var item in Ids)
                {
                    if (Icodes.Contains(item))
                    {
                        Icodes = Icodes.Replace(item, "");
                    }
                    else
                    {
                        listDetail.Add(dataDetail.FirstOrDefault(t => t.InspectionItemId == item));
                    }
                }
            }
            if (string.IsNullOrEmpty(model.Code)) model.Code = null;
            billbll.SaveForm(keyValue, model, Icodes, Inames, listDetail);
            return Success("操作成功。");
        }
    }
}
